<?php
/*
	分销中心
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$return['setting']=$this->plugin_setting('page_setting');
	set_html('title',$return['setting']['title']?:'分销中心');
	$basic=model('core/member')->basic($_SESSION['uid']);
	$member=array_merge($member,$basic);
	if($member['agent_id']){
		$member['agent']=model('core/member')->basic($member['agent_id']);
	}else{
		$member['agent']=array('nickname'=>'总店');
	}
	if($member['level_id']){
		$level=$this->get('level',array('id'=>$member['level_id']));
		$commissions=json_decode($level['commissions'],true);
		$level['zhekou']=$commissions['zhekou'];
		$member['level']=$level['title'];
	}else{
		$member['level']='普通等级';
	}
	$member['agent_typetitle']=$this->agent_types[$member['type']];
	$return['ordernum']=0;
	$return['level']=$level;
	for($i=0;$i<$this->setting['level'];$i++){
		$level=$i+1;
		$return['ordernum'] +=$this->count('commission',array('mid'=>$_SESSION['uid'],'level'=>$level));
	}
	$return['member']=$member;
	$data=array();
	//已结算佣金
	$data['balance_total']=$this->sum('balance',array('mid'=>$_SESSION['uid']),'money');
	$data['balance_num']=$this->count('balance',array('mid'=>$_SESSION['uid']));
	//待结算佣金
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>array(0,1));
	/*if($this->setting['balance_day']){
		//结算天数
		$time=TIMESTAMP-intval($this->setting['balance_day'])*86400;
		$params['complete_time <']=$time;
	}*/
	$data['upfee_1']=$this->sum('commission',$params,'money');
	$data['upfee_total']=$this->sum('commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'money');
	$params['status']=1;
	$data['commission_1']=$this->sum('commission',$params,'money');
	$data['commission_total']=$this->sum('commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'money');
	if($return['pluginset']['is_total']){
		$data['myorders']=pdo_getsum('jy_weishop_order_goods',array('ispay'=>1,'mid'=>$_SESSION['uid']),'total');
	}else{
		$data['myorders']=pdo_count('jy_weishop_order',array('ispay'=>1,'mid'=>$_SESSION['uid']));
	}
	$agent_ids=$this->getall('member',array('agent_id'=>$_SESSION['uid']),array('mid'),'mid');
	if(count($agent_ids)){
			$agent_ids=array_keys($agent_ids);
			if($return['pluginset']['is_total']){
				$data['myteamorders']=pdo_getsum('jy_weishop_order_goods',array('ispay'=>1,'mid'=>$agent_ids),'total');
			}else{
				$data['myteamorders']=pdo_count('jy_weishop_order',array('ispay'=>1,'mid'=>$agent_ids));
			}
	}
	$data['myteamorders'] +=$data['myorders'];
	//
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']);
	$params['type']=0;
	$data['fee0']=$this->sum('commission',$params,'money');
	$params['type']=1;
	$data['fee1']=$this->sum('commission',$params,'money');
	$params['type']=2;
	$data['fee2']=$this->sum('commission',$params,'money');
	$params['type']=3;
	$data['fee3']=$this->sum('commission',$params,'money');
	$params['type']=4;
	$data['fee4']=$this->sum('commission',$params,'money');
	$params['type']=5;
	$data['fee5']=$this->sum('commission',$params,'money');
	$params['type']=6;
	$data['fee6']=$this->sum('commission',$params,'money');
	$params['type']=7;
	$data['fee7']=$this->sum('commission',$params,'money');
	$params['type']=8;
	$data['fee8']=$this->sum('commission',$params,'money');
	$params['type']=9;
	$data['fee9']=$this->sum('commission',$params,'money');
	$return['data']=$data;
	$return['rank']=$this->plugin_setting('rank');
	exi($return);
}
if($_RQ['op']=='apply'){
	$apply=$this->get('apply',array('mid'=>$_SESSION['uid']));
	if($apply['status']==0 && $apply['id']){
		exi('您已申请，请等待审核！','error');
	}
		$post=get_postdata();
		if(!$post['agree']){
			exi('请先阅读协议！','error');
		}
		if(!$post['realname']){
			exi('请输入姓名！','error');
		}
		if(!$post['mobile']){
			exi('请输入联系电话！','error');
		}
		$member_update=array(
			'realname'=>trim($post['realname']),
			'mobile'=>trim($post['mobile']),
		);
		if($post['wechat']){
			$member_update['wechat']=trim($post['wechat']);
		}
		model('core/member')->member_update($_SESSION['uid'],$member_update);
		if($post['agent_id']){
			$agent_id=intval($post['agent_id']);
			if($agent_id){
				$this->model('member')->set_parent($_SESSION['uid'],$agent_id);
			}
		}
		$this->model('member')->apply_agent($_SESSION['uid']);
		exi('申请成功！请耐心等待管理员审核！','',custom_url('jy_shopnewfee/index/',true));
}
?>